package com.tjvib.adapter;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.tjvib.R;
import com.tjvib.TJVIBAPP;
import com.tjvib.bean.SensorBean;
import com.tjvib.util.CopyUtil;
import com.tjvib.util.LogUtil;
import com.tjvib.util.Manager.UserManager;
import com.tjvib.util.PressUtil;
import com.tjvib.util.ToastUtil;
import com.tjvib.view.activity.DeviceActivity;
import com.tjvib.view.activity.LocalSensorActivity;
import com.tjvib.view.activity.ZeroLabDeviceActivity;
import com.tjvib.view.dialog.HintDialog;

import java.util.List;

public class SensorAdapter extends BaseQuickAdapter<SensorBean, BaseViewHolder> {

    private Context context;

    public SensorAdapter(int layoutResId, Context context) {
        super(layoutResId);
        this.context = context;
    }

    public SensorAdapter(int layoutResId, @Nullable List<SensorBean> data, Context context) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, SensorBean sensorBean) {
        Glide.with(context)
                .load(sensorBean.getPic_url())
                .into((ImageView) baseViewHolder.findView(R.id.item_sensor_iv_pic));

        baseViewHolder.setVisible(R.id.item_sensor_tv_buy, sensorBean.isIs_buy());

        baseViewHolder.setText(R.id.item_sensor_tv_name, sensorBean.getName())
                .setText(R.id.item_sensor_tv_con, sensorBean.getCon());

        baseViewHolder.findView(R.id.item_sensor_tv_buy).setOnClickListener(v -> {
            showBuyDialog(sensorBean);
        });

        baseViewHolder.findView(R.id.item_sensor_tv_choose).setOnClickListener(v -> {
            if (sensorBean.getName().equals("手机自带传感器")) {
                context.startActivity(new Intent(context, LocalSensorActivity.class));
            } else if (sensorBean.getName().equals("LPMS-B2传感器")) {
                context.startActivity(new Intent(context, DeviceActivity.class));
            }
        });

        PressUtil.setPressChange(context, baseViewHolder.findView(R.id.item_sensor_tv_buy),
                baseViewHolder.findView(R.id.item_sensor_tv_choose), baseViewHolder.findView(R.id.item_sensor_rl));

        baseViewHolder.findView(R.id.item_sensor_rl).setOnClickListener(v -> {
            if (sensorBean.getName().equals("手机自带传感器")) {
                context.startActivity(new Intent(context, LocalSensorActivity.class));
            } else if (sensorBean.getName().equals("LPMS-B2传感器")) {
                if (BluetoothAdapter.getDefaultAdapter() == null)
                    showBlueToothDialog("没有发现蓝牙模块", "非常抱歉，没有发现蓝牙模块，无法使用本功能。");
                else
                    context.startActivity(new Intent(context, DeviceActivity.class));
            }else if (sensorBean.getName().equals("ZeroLab传感器")) {
                if (BluetoothAdapter.getDefaultAdapter() == null)
                    showBlueToothDialog("没有发现蓝牙模块", "非常抱歉，没有发现蓝牙模块，无法使用本功能。");
                //判断是否大于android 9.0版本
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    context.startActivity(new Intent(context, ZeroLabDeviceActivity.class));
                } else {
                    showBlueToothDialog("不支持设备蓝牙版本", "非常抱歉，本功能需要本设备蓝牙版本5.0及以上。");
                }
            }
        });
    }


    private void showBuyDialog(SensorBean sensorBean) {
        HintDialog hintDialog = new HintDialog(context);
        hintDialog.setConfirmText("发送");
        hintDialog.showDialog(context, "购买" + sensorBean.getName(),
                "请联系tsmartvib@163.com说明购买意图，我们收到后会尽快回复您。", view -> {
                    sendEmail(sensorBean);
                    if (hintDialog.isShowing())
                        hintDialog.dismiss();
                },
                view -> {
                    if (hintDialog.isShowing())
                        hintDialog.dismiss();
                });
    }


    private void sendEmail(SensorBean sensorBean) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"tsmartvib@163.com"});
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.email_subject_buy_sensor));
        intent.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.email_text_buy_sensor,
                UserManager.getInstance().getUserId(), sensorBean.getName()));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        } else {
            ToastUtil.show("当前设备似乎没有邮箱app，邮箱地址已复制成功，请您自行发送邮件");
            CopyUtil.getInstance(context).CopyText("tsmartvib@163.com");
        }
    }


    private void showBlueToothDialog(String tit, String msg) {
        HintDialog hintDialog = new HintDialog(context);
        hintDialog.setConfirmText("好的");
        hintDialog.showDialog(context, tit, msg,
                view -> {
                    if (hintDialog.isShowing())
                        hintDialog.dismiss();
                },
                view -> {
                    if (hintDialog.isShowing())
                        hintDialog.dismiss();
                });
    }



}
